package com.tuanzi.loan.business.repository.system.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanPartner.LOAN_PARTNER;

import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.repository.system.custom.PartnerRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.system.PartnerSearchRequest;
import com.tuanzi.loan.vo.response.system.PartnerSearchResponse;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;

/**
 * @author Ray 2017/09/21
 */
@Repository
@SuppressWarnings({ "rawtypes" })
public class PartnerRepositoryImpl extends JOOQBaseRepository implements PartnerRepositoryCustom {

	@Override
	protected TableImpl getTable() {
		return LOAN_PARTNER;
	}

	@Override
	public Page<PartnerSearchResponse> list(PartnerSearchRequest searchRequest, Pageable pageable) {
		contains(LOAN_PARTNER.NAME, searchRequest.getName());
		contains(LOAN_PARTNER.PHONE, searchRequest.getPhone());
		contains(LOAN_PARTNER.ADDRESS, searchRequest.getAddress());
		eq(LOAN_PARTNER.ENABLE, searchRequest.getEnable());
		return search(pageable, new PartnerSearchResponseRecordMapper());
	}

	private final class PartnerSearchResponseRecordMapper implements RecordMapper<Record, PartnerSearchResponse> {

		@Override
		public PartnerSearchResponse map(Record record) {
			PartnerSearchResponse response = new PartnerSearchResponse();
			response.setId(record.get(LOAN_PARTNER.ID));
			response.setAddress(PhoneNumNideUtil.hideAddress(record.get(LOAN_PARTNER.ADDRESS)));
			response.setName(record.get(LOAN_PARTNER.NAME));
			response.setEnable(record.get(LOAN_PARTNER.ENABLE));
			response.setPhone(PhoneNumNideUtil.hideNum(record.get(LOAN_PARTNER.PHONE)));
			return response;
		}
	}
}
